from main import MainUi
import sys, base64, sip
import qtawesome, requests, re
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCursor, QImage, QPixmap
from PyQt5.QtWidgets import QColorDialog,QFontDialog
from io import BytesIO

class Right_UI(MainUi):
    def ico(self,req_in):
        photo = QtGui.QPixmap()
        photo.loadFromData(req_in)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(photo))
        return icon

    def left_color_select(self,LOR):  # 颜色
        self.select = QColorDialog.getColor(Qt.gray)
        if self.select.isValid() and LOR == 'L':
            self.color_L = self.select.name()
            self.left_set_tools_UI()
        elif self.select.isValid() and LOR == 'R':
            self.color_R = self.select.name()
            self.right_set_tools_UI()

    def left_font_select(self,LOR):  # 字体
        self.fond,ok = QFontDialog.getFont()
        if ok and LOR == 'L':
            self.size_L = f'{self.fond.pointSize()}'
            self.family_L = f'{self.fond.family()}'
            self.weight_L = f'{self.fond.weight()*10}'
            self.left_set_tools_UI()
        elif ok and LOR == 'R':
            self.size_R = f'{self.fond.pointSize()}'
            self.family_R = f'{self.fond.family()}'
            self.weight_R = f'{self.fond.weight()*10}'
            print(self.weight_R)
            self.right_set_tools_UI()

    def main_toum(self):
        self.num=int(self.right_toum_QSlider.value())
        self.setWindowOpacity(self.num/10)  # 设置窗口透明度




    def set_tools_win(self):
        self.right_secede_ui()
        self.right_set_widget = QtWidgets.QWidget()  # 创建右侧部件
        self.right_set_widget.setObjectName('right_set_widget')
        self.right_set_layout = QtWidgets.QGridLayout()
        self.right_set_widget.setLayout(self.right_set_layout)  # 设置右侧部件布局为网格

        self.right_set_button_widget = QtWidgets.QWidget()  # 创建右侧部件
        self.right_set_button_widget.setObjectName('right_set_button_widget')
        self.right_set_button_layout = QtWidgets.QGridLayout()
        self.right_set_button_widget.setLayout(self.right_set_button_layout)  # 设置右侧部件布局为网格

        self.pags.append('self.right_set_widget')
        self.pags.append('self.right_set_button_widget')

        self.right_color_label=QtWidgets.QLabel('阅读页颜色:')
        self.right_color_label.setObjectName('set_lable')
        self.left_color_label = QtWidgets.QLabel('左侧颜色:')
        self.left_color_label.setObjectName('set_lable')
        self.right_font_label=QtWidgets.QLabel('阅读页字体:')
        self.right_font_label.setObjectName('set_lable')
        self.left_font_label = QtWidgets.QLabel('左侧字体:')
        self.left_font_label.setObjectName('set_lable')
        self.right_toum_label = QtWidgets.QLabel('透明度:')
        self.right_toum_label.setObjectName('set_lable')
        self.right_white_label=QtWidgets.QLabel('')
        self.right_white_label.setObjectName('set_lable')
        self.right_checkBox=QtWidgets.QCheckBox('桌面快捷方式')


        self.right_color_button=QtWidgets.QPushButton(qtawesome.icon('fa.th', color='white'),'')
        self.right_color_button.setFixedSize(50, 25)
        self.left_color_button=QtWidgets.QPushButton(qtawesome.icon('fa.th', color='white'),'')
        self.left_color_button.setFixedSize(50, 25)
        self.right_font_button=QtWidgets.QPushButton(qtawesome.icon('fa.font', color='white'),'')
        self.right_font_button.setFixedSize(50, 25)
        self.left_font_button=QtWidgets.QPushButton(qtawesome.icon('fa.font', color='white'),'')
        self.left_font_button.setFixedSize(50, 25)

        self.right_back_button=QtWidgets.QPushButton(qtawesome.icon('fa.reply', color='black'),'返回')
        self.right_back_button.setIconSize(QtCore.QSize(15, 15))
        self.right_back_button.setObjectName('right_back_button')

        self.right_toum_QSlider=QtWidgets.QSlider(Qt.Horizontal)
        self.right_toum_QSlider.setMinimum(1)  # 最小值
        self.right_toum_QSlider.setMaximum(10)  # 最大值
        self.right_toum_QSlider.setSingleStep(1)  # 步长
        self.right_toum_QSlider.setTickPosition(QtWidgets.QSlider.TicksBelow)  # 设置刻度位置，在下方
        self.right_toum_QSlider.setTickInterval(1)  # 设置刻度间隔
        self.right_toum_QSlider.setValue(int(self.num))
        self.right_toum_QSlider.valueChanged.connect(self.main_toum)

        self.right_color_button.clicked.connect(lambda:self.left_color_select(LOR='R'))
        self.right_font_button.clicked.connect(lambda:self.left_font_select(LOR='R'))
        self.left_color_button.clicked.connect(lambda:self.left_color_select(LOR='L'))
        self.left_font_button.clicked.connect(lambda:self.left_font_select(LOR='L'))

        self.right_back_button.clicked.connect(lambda:self.pag_back(self.right_secede_widget))
        self.right_secede_layout.addWidget(self.right_set_widget, 0, 0, 25, 60)  # 右侧部件在第0行第3列，占8行9列
        self.right_secede_layout.addWidget(self.right_set_button_widget, 26, 0, 1, 60)  # 右侧部件在第0行第3列，占8行9列

        self.right_set_layout.addWidget(self.right_color_label,1,1,1,2)
        self.right_set_layout.addWidget(self.left_color_label,1,5,1,2)
        self.right_set_layout.addWidget(self.right_font_label,2,1,1,2)
        self.right_set_layout.addWidget(self.left_font_label,2,5,1,2)
        self.right_set_layout.addWidget(self.right_toum_label,3,1,1,2)
        self.right_set_layout.addWidget(self.right_checkBox,4,1,1,2)
        self.right_set_layout.addWidget(self.right_white_label,20,1,1,77)
        self.right_set_layout.addWidget(self.right_color_button,1,3,1,2)
        self.right_set_layout.addWidget(self.left_color_button,1,7,1,2)
        self.right_set_layout.addWidget(self.right_font_button,2,3,1,2)
        self.right_set_layout.addWidget(self.left_font_button,2,7,1,2)
        self.right_set_layout.addWidget(self.right_toum_QSlider,3,3,1,10)
        self.right_set_button_layout.addWidget(self.right_back_button,0,1,3,60)

        self.right_set_button_widget.setStyleSheet('''
                                                QPushButton#right_back_button:hover{
                                                color:black;
                                                height:30px;
                                                border-bottom:4px solid red;
                                                font-weight:700;
                                                background:white;
                                                border-bottom-right-radius:10px;}
                                                QPushButton#right_back_button{
                                                height:30px;
                                                background:white;
                                                color:black;
                                                font-weight:700;
                                                border-bottom-right-radius:10px;}
                                                font-size:18px;
                                                color:white;
                                                weight:600;}
                                                QWidget#right_set_widget{
                                                background:white;
                                                border-top:1px solid darkGray;
                                                border-bottom:1px solid darkGray;
                                                border-right:1px solid darkGray;
                                                border-left:1px solid darkGray;
                                                border-bottom-right-radius:10px;
                                                }
                                                ''')

        self.right_set_widget.setStyleSheet('''
                                        QLabel#set_lable{font-size:18px;color:white;font-weight:800;}
                                        QWidget#right_set_widget{
                                        background:#717174;
                                        border-top:1px solid darkGray;
                                        border-bottom:1px solid darkGray;
                                        border-right:1px solid darkGray;
                                        border-left:1px solid darkGray;
                                        border-top-right-radius:10px;
                                        }
                                        ''')

    def left_set_tools_UI(self):
        self.left_widget.setStyleSheet('''
                                QPushButton{border:none;color:white;}
                                QPushButton#left_label{
                                border:none;
                                border-bottom:1px solid white;
                                font-size:%spt;
                                font-weight:%s;
                                font-family: "%s", Helvetica, Arial, sans-serif;
                                }
                                QPushButton#left_button:hover{border-left:4px solid red;font-weight:700;}
                                QWidget#left_widget{
                                background:%s;
                                border-top:1px solid white;
                                border-bottom:1px solid white;
                                border-left:1px solid white;
                                border-top-left-radius:10px;
                                border-bottom-left-radius:10px;
                                }
                                ''' % (self.size_L, self.weight_L, self.family_L, self.color_L))
    def right_set_tools_UI(self):
        self.right_book_read_widget.setStyleSheet('''QTextEdit#right_widget_read_book{
                                font-size:%spt;
                                font-wight:%s;
                                font-family:"%s", Helvetica, Arial, sans-serif;
                                background:%s;
                                }
                                '''% (self.size_R, self.weight_R, self.family_R, self.color_R))
